<template>
  <div class="container">
    <el-card
      class="container__card"
      shadow="hover"
    >
      <template #header>
        <div class="card-header">
          <text class="title">
            登录/LOGIN
          </text>
        </div>
      </template>
      <el-form
        status-icon
        ref="lognFormRef"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <el-form-item
          prop="username"
          ref="usernameRef"
        >
          <el-input
            :prefix-icon="User"
            clearable
            @clear="clearFormItem(usernameRef)"
            v-model="loginForm.username"
            placeholder="用户名"
            type="text"
          />
        </el-form-item>
        <el-form-item
          prop="password"
          ref="passwordRef"
        >
          <el-input
            :prefix-icon="Lock"
            clearable
            @clear="clearFormItem(passwordRef)"
            v-model="loginForm.password"
            placeholder="请输入密码"
            :type="passwordType"
          >
            <template #suffix>
              <el-icon
                class="suffix"
                @click.stop="switchType"
              >
                <icon-ep-hide v-if="passwordType == 'password'" />
                <icon-ep-view v-else />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item
          prop="code"
        >
          <div class="space-between">
            <el-input
              style="width: 200px;"
              clearable
              v-model="loginForm.code"
              placeholder="请输入验证码"
              type="text"
            />
            <span
              class="code"
              @click="getCode"
              v-html="codeUrl"
            />
          </div>
        </el-form-item>
        <el-form-item>
          <div class="space-between">
            <el-button
              link
              text
              type="primary"
            >
              注册
            </el-button>
            <el-button
              link
              text
              type="danger"
            >
              忘记密码？
            </el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            round
            class="login-button"
            type="primary"
            @click="submitForm(lognFormRef)"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, getCurrentInstance } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules, FormItemInstance } from 'element-plus'
import { getVerifyCode, userLogin } from '@/api/user'
import { useUser } from '@/store/modules/user'
const { proxy } = (getCurrentInstance() as any)
const passwordType = ref('password')
const lognFormRef = ref()
const usernameRef = ref()
const passwordRef = ref()
const codeUrl = ref<string>('')
const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
  code: ''
})
// 表单验证规则
const loginFormRules = reactive<FormRules<LoginForm>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 8, message: '用户名3-8个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_]{6,15}$/, message: '大小写字母及数字下划线', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
})
// 切换密码显示隐藏
const switchType = (): void => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}
// 获取验证码
const getCode = (): void => {
  getVerifyCode().then((data: any) => {
    codeUrl.value = data
  })
}
// 提交登录
async function submitForm (formRef: FormInstance): Promise<void> {
  await formRef.validate((valid: boolean) => {
    if (valid) {
      userLogin(loginForm).then((res: any) => {
        if ((res)) {
          const store = useUser()
          store.setToken(res.token)
          store.setUserInfo(res.user)
          proxy.$router.push('/home')
        }
      }).catch((err: any) => {
        getCode()
        proxy.$message.warning(err.message)
      })
    } else {
      return false
    }
  })
}
// 重置指定表单项
const clearFormItem = (el: FormItemInstance): void => {
  el.resetField()
}
getCode()
</script>
<style lang="scss" scoped>
.container {
    width: 100vw;
    height: 100vh;
    // background: $color-content;
    background: url('@/assets/images/login-bg.png') no-repeat center;
    background-size: cover;
    background-attachment: fixed;
    display: flex;
    justify-content: center;
    align-items: center;

    .container__card {
        width: 400px;
        // height: 330px;
        padding: 0 10px;
        background: rgba(255, 255, 255, 0.6);
        box-shadow: 0 0 50px 5px #FFAB40;
        .card-header {
            text-align: left;
            border-color: $fg-primary !important;
            .title {
                font-size: 18px;
                color: $bg-primary;
            }
        }
        .code {
          width: 110px;
          height: 35px;
          cursor: pointer;
        }
        .suffix {
            // width: 60px;
            height: 100%;
            cursor: pointer;
        }
        .login-button {
            margin: 0 auto;
            width:100%;
            // height: 40px;
            color: $fg-primary;
        }
    }
}
</style>
